<?php
/*
* 大客户经理接口封装
* @author  chendm<402197974@qq.com> 
* @date  2017-08-31 16:53:00
* 
*/
class  Manager extends Admin_Controller {
	function __construct()
   {
      parent::__construct();

      $this->load->helper('url_helper');
      $this->load->helper('url');
      $this->load->model('users_model');
      $this->load->model('manager_model');

      error_reporting(0);
   }

   //大客户经理销售排行榜
   public function manager_top(){
      //初始化参数
      $id = $_GET['id'];
      $type =  (isset($_GET['type']) && !empty($_GET['type'])) ? $_GET['type'] : 'season';
      $limit = (isset($_GET['limit']) && !empty($_GET['limit'])) ? $_GET['limit'] : 3;
      //验证参数
      if($id){
          //封装数据
          $data['status'] = "success";
          $data['msg'] = "获取成功";
          $data['data'] = $this->manager_model->getManagerTop($id,$type,$limit);
      }else{
          $data['status'] = "fail";
          $data['msg'] = "参数错误";
          $data['data'] = "";
      }
     
      // echo '<pre/>';print_r($data);exit;
      echo json_encode($data);
   }

   //大客户经理管理列表
   public function manager_list(){
      //初始化参数
      $id = $_GET['id'];
      $status =  (isset($_GET['status']) && !empty($_GET['status'])) ? $_GET['status'] : 0;
      $sort_type = (isset($_GET['sort_type'])  && !empty($_GET['sort_type'])) ? $_GET['sort_type'] : 1;
      $asc_type = (isset($_GET['asc_type']) && !empty($_GET['asc_type'])) ? $_GET['asc_type'] : '';
      $limit = (isset($_GET['limit']) && !empty($_GET['limit'])) ? $_GET['limit'] : 3;
      $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
      $search = isset($_GET['search']) ? $_GET['search'] : '';
      //验证参数
      if($id){
          //封装数据
          $data['status'] = "success";
          $data['msg'] = "获取成功";
          $data['data'] = $this->manager_model->getManagerList($id,$status,$sort_type,$asc_type,$limit,$page,$search);
      }else{
          $data['status'] = "fail";
          $data['msg'] = "参数错误";
          $data['data'] = "";
      }
      // echo '<pre/>';print_r($data);exit;
      // header("Access-Control-Allow-Origin: http://runapi.showdoc.cc");
      // header("Access-Control-Allow-Credentials : true");
      echo json_encode($data);
   }

   //变更大客户经理状态
   public function manager_update_status(){
   	  $post = $this->input->post();
      // $post['id'] = 1;
      // $post['status'] = 2;
      switch ($post['status']){
          case '1':
              $status =  '正常状态';
              break;
          case '2':
              $status =  '已离职';
              break;
          case '3':
              $status =  '培训中';
              break;
          default:
              $status = '';
              break;
      }
      if (isset($post['id']) && isset($status)){
          $res = $this->manager_model->updateStatus($post['id'],$status);
          if ($res > 0){
              $data['status'] = "success";
              $data['msg'] = "更新成功";
              $data['data'] = "";
          }else{
              $data['status'] = "fail";
              $data['msg'] = "更新失败!";
              $data['data'] = "";
          }
      }else{
          $data['status'] = "fail";
          $data['msg'] = "参数错误";
          $data['data'] = "";
      }
      // echo '<pre/>';print_r($data);exit;
      echo json_encode($data);
   }


   //大客户经理详情
   public function manager_detail(){
      //初始化参数
      $id = $_GET['id'];
      $pid = $_GET['pid'];
       //验证参数
      if($id && $pid){
          //封装数据
          $data['status'] = "success";
          $data['msg'] = "获取成功";
          //维护记录
          $page = $_GET['page']?$_GET['page']:1;
          $limit = $_GET['limit']?$_GET['limit']:3;
          $data['data'] = $this->manager_model->getManagerDetail($id,$pid,$page,$limit);
      }else{
          $data['status'] = "fail";
          $data['msg'] = "参数错误";
          $data['data'] = "";
      }
      // echo '<pre/>';print_r($data);exit;
      echo json_encode($data);
   }

    //跟踪数据统计
    public function manager_statistics(){
        $id = $_GET['id'];
        $year = isset($_GET['year']) ? $_GET['year'] : date("Y");
        $month = isset($_GET['month']) ? $_GET['month'] : date("m");
        if($id){
          $data['status'] = "success";
          $data['msg'] = "获取成功";
          $data['data'] = $this->manager_model->getStatistics($id,$year.'-'.$month);
        }else{
          $data['status'] = "fail";
          $data['msg'] = "参数错误";
          $data['data'] = "";
        }
        // echo '<pre/>';print_r($data);exit;
        echo json_encode($data);
    }


    //修改业绩指标
    public function norm_update(){
      $post = $this->input->post();
      // $post['manager_id'] = 72; //大客户经理id
      // $post['next_performance'] = 110.00;
      // $post['next_recruit'] = 10;
      if (isset($post['manager_id']) && isset($post['next_performance']) && isset($post['next_recruit'])){
        //更新大客户经理下月业绩数据
        $res = $this->manager_model->updateNorm($post['manager_id'],date('Y-m', strtotime("+1 month")),$post['next_performance'],$post['next_recruit']);
        if ($res){
            $data['status'] = "success";
            $data['msg'] = "更新成功";
            $data['data'] = "";
        }else{
            $data['status'] = "fail";
            $data['msg'] = "更新失败!";
            $data['data'] = "";
        }
      }else{
          $data['status'] = "fail";
          $data['msg'] = "参数错误";
          $data['data'] = "";
      }
      // echo '<pre/>';print_r($data);exit;
      echo json_encode($data);
    }

    //修改维护记录
    public function record_update(){
      $post = $this->input->post();
      // $post['id'] = 4;
      // $post['content'] = '测试修改维护记录';
      if (isset($post['id']) && isset($post['content'])){
          $res = $this->db->update('manager_record',array('content'=>$post['content']),array('id'=>$post['id']));
          if ($res){
              $data['status'] = "success";
              $data['msg'] = "更新成功";
              $data['data'] = "";
          }else{
              $data['status'] = "fail";
              $data['msg'] = "更新失败!";
              $data['data'] = "";
          }
      }else{
          $data['status'] = "fail";
          $data['msg'] = "参数错误";
          $data['data'] = "";
      }
      // echo '<pre/>';print_r($data);exit;
      echo json_encode($data);
    }

    //删除维护记录
    public function record_delete(){
      $post = $this->input->post();
      // $post['id'] = 4;
      if (isset($post['id'])){
          $res = $this->db->delete('manager_record',array('id'=>$post['id']));
          if ($res){
              $data['status'] = "success";
              $data['msg'] = "删除成功";
              $data['data'] = "";
          }else{
              $data['status'] = "fail";
              $data['msg'] = "删除失败!";
              $data['data'] = "";
          }
      }else{
          $data['status'] = "fail";
          $data['msg'] = "参数错误";
          $data['data'] = "";
      }
      // echo '<pre/>';print_r($data);exit;
      echo json_encode($data);
    }

    // 用户上级信息
    public function manager_higher_info_api()
    {
        $manager_list = $this->manager_model->manager_higher_info();
        $manager_list = array_group_by($manager_list,'id');
        echo json_encode($manager_list);

    }

    // 通过销售经理名字能查询到职位的接口
    public function query_manager_identity()
    {
        if(isset($_GET['name']) && $_GET['name'])
        {
            $name = $_GET['name'];
            $sql = "select identity from manager WHERE name = '$name'";
            $res = $this->db->query($sql)->row_array();
            $data['status'] = "success";
            $data['msg'] = "查询成功";
            $data['data'] = $res;
        }else{
            $data['status'] = "fail";
            $data['msg'] = "参数错误";
            $data['data'] = "";
        }
        echo json_encode($data);
    }


}